$(function() {
	
	$("#multiMedia_add_picture").click(function() {
		multiMedia_add_picture();
	});
	
	$("#multiMedia_add_video").click(function() {
		multiMedia_add_video();
	});
	
	$("#multiMedia_add_voice").click(function() {
		multiMedia_add_voice();
	});
	
	$("#multiMedia_add_sub").click(function() {
		multiMedia_add_sub();
	});
	getMediasNew();
});

var multiMedia_add_picture_i = 2;
function multiMedia_add_picture(){
	var tr = $('<tr>'+
		'<td><input type="text" style="width:100%" readonly="readonly" name="picture_address_'+multiMedia_add_picture_i+'"/></td>'+
		'<td class="text-center">'+
			'<a class="red" style="cursor: pointer;" title="删除" onclick="multiMedia_del($(this))">'+
					'<i class="ace-icon fa fa-trash-o bigger-130"></i>'+
			'</a>'+
		'</td>'+
	'</tr>');
	$("#picture_tbody").append(tr);
	
	multiMedia_add_picture_i ++;
}

var multiMedia_add_video_i = 2;
function multiMedia_add_video(){
	var tr = $('<tr>'+
		'<td><input type="text" style="width:100%" readonly="readonly" name="video_address_'+multiMedia_add_video_i+'"/></td>'+
		'<td class="text-center">'+
			'<a class="red" style="cursor: pointer;" title="删除" onclick="multiMedia_del($(this))">'+
					'<i class="ace-icon fa fa-trash-o bigger-130"></i>'+
			'</a>'+
		'</td>'+
	'</tr>');
	$("#video_tbody").append(tr);
	
	multiMedia_add_video_i ++;
}

var multiMedia_add_voice_i = 2;
function multiMedia_add_voice(){
	var tr = $('<tr>'+
		'<td><input type="text" style="width:100%" readonly="readonly" name="voice_address_'+multiMedia_add_voice_i+'"/></td>'+
		'<td class="text-center">'+
			'<a class="red" style="cursor: pointer;" title="删除" onclick="multiMedia_del($(this))">'+
					'<i class="ace-icon fa fa-trash-o bigger-130"></i>'+
			'</a>'+
		'</td>'+
	'</tr>');
	$("#voice_tbody").append(tr);
	
	multiMedia_add_voice_i ++;
}

/**
 * 删除行
 * @param delBtn
 */
function multiMedia_del(delBtn){
	delBtn.parent().parent().remove();
}

/**
 * 提交信息
 */
function multiMedia_add_sub(){
	var option = {
			dataType : 'json',
			data : {wetlandParkId : wetlandParkId},
			success : function(value) {
				if (value == true) {
					multiMedia.html(multiMedia.text() + successHTML);
					$.gritter.add({
						title : '确认消息',
						text : '恭喜您,信息添加成功！',
						class_name : 'gritter-success'
					});
				} else {
					multiMedia.html(multiMedia.text() + warnHTML);
					$.gritter.add({
						title : '确认消息',
						text : '对不起,信息添加失败！',
						class_name : 'gritter-error'
					});
				}
			},
			error : function() {
				multiMedia.html(multiMedia.text() + warnHTML);
				$.gritter.add({
					title : '服务器错误',
					text : '对不起，服务器错误，请联系系统管理员！',
					class_name : 'gritter-error'
				});
			}
		};
		$("#multiMedia_form").ajaxForm(option).submit();
}

function getMediasNew(){
	var id =$("#wetlandParkId").val();
	$.ajax({
		url : "findbyid",
		dataType : "json",
		type : "POST",
		data : {id:id},
		async : false,
		error : function(error) {
			console.log(error.responseText);
			$.gritter.add({
				title : '服务器错误',
				text : '对不起，服务器错误，请联系系统管理员！',
				class_name : 'gritter-error'
			});

		},
		success : function(strValue) {
			if (strValue != null) {
				var arr1 = strValue.wlin_picture;
				var arr2 = strValue.wlin_video;
				var arr3 = strValue.wlin_voice;
				for(var i=0;i<arr1.length;i++){		
					$("#picture_tbody").append('<tr><td><a data-lightbox="categroy" data-title="" href='+arr1[i]+'><img src='+arr1[i]+' width="300" height="150"></a></td>'+
								'</tr>');
				}	
				
				for(var i=0;i<arr2.length;i++){		
					$('#video_tbody').append('<tr><td><video controls width="300" height="150"><source src='+arr2[i]+' type="video/mp4"></video></td>'+
							'</tr>');		
				}
				
				for(var i=0;i<arr3.length;i++){		
					$('#voice_tbody').append('<tr><td style="width:300px"><audio controls><source src='+arr3[i]+' type="audio/mpeg"></audio></td>'+
							'</tr>');
				}
			}
		}
	});
}

function getMedias(){
	var id =$("#wetlandParkId").val();
	$.ajax({
		url : "findbyid",
		dataType : "json",
		type : "POST",
		data : {id:id},
		async : false,
		error : function(error) {
			console.log(error.responseText);

			$.gritter.add({
				title : '服务器错误',
				text : '对不起，服务器错误，请联系系统管理员！',
				class_name : 'gritter-error'
			});

		},
		success : function(strValue) {
			if (strValue != null) {
				if(strValue.wlin_picture.length>=1){
					$("input[name='picture_address_1']").val(strValue.wlin_picture[0]);
					
					for(var i=2;i<strValue.wlin_picture.length+1;i++){
						multiMedia_add_picture();
						$("input[name='picture_address_"+i+"']").val(strValue.wlin_picture[i-1]);
					
					}
				}
				
				if(strValue.wlin_video.length>=1){
					$("input[name='video_address_1']").val(strValue.wlin_video[0]);
					
					for(var i=2;i<strValue.wlin_video.length+1;i++){
						multiMedia_add_video();
						$("input[name='video_address_"+i+"']").val(strValue.wlin_video[i-1]);
					
					}
				}
				
				if(strValue.wlin_voice.length>=1){
					$("input[name='voice_address_1']").val(strValue.wlin_voice[0]);
					
					for(var i=2;i<strValue.wlin_voice.length+1;i++){
						multiMedia_add_voice();
						$("input[name='voice_address_"+i+"']").val(strValue.wlin_voice[i-1]);
					}
				}
			}
		}
	});
}


